//**********************************************
//**********************************************
//
//      DEBUG in http://jsfiddle.net
//
//**********************************************
/***********************************************
<!---------------------------------------------->
<!-- url menu ---------------------------------->
<!---------------------------------------------->
<div id="url-div" style="margin:-0em 0 0 0">
<a href="#">yaimg</a>  /  <a href="#">ibreast</a>  /  <a href="#">erotiqueeee</a>
</div>
/*
  $('#url-div a').click(function(e) {
    var txt = $(e.target).text();
    //alert(txt);
    $("input#txt").val(txt);
  });
************************************************/

/*
<!---------------------------------------------->
<!-- button & text box ------------------------->
<!---------------------------------------------->
<div style="margin:-0em 0 0 0">
<p align=center style='margin:0'>
<input type=button value='(O.O)' id='buttonId' style="width:50% ;margin:0 auto">   <input type=text style="width:5em" id='txt' value='yaimg'></p>
<div id="viewer" style="border:2px #ddd solid; font-size:9px; margin:1em 0 3em 0; padding:0; background-color:#ccc">
</div>
</div>
*/

///////////////////////////////////////////////////////////////////////////
// view button click //////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
/*
$("input#buttonId").click(function (){
	$("input#buttonId").attr("disabled", true); // prevent double click
	// button enable after 3 sec
	var prevent_timer = setInterval(function () {
		clearInterval(prevent_timer);
		$("input#buttonId").attr("disabled", false);
		}, 3000);
	$("input#buttonId").blur(); // button remove focus
	//location.hash= "#vcntnr";
	Tmblr_OneSite_RandomView($("input#txt").val());
});
*/

var prev1tmbrUrl = "";
var prev2tmbrUrl = "";

function Tmblr_OneSite_RandomView(url) {
    //alert(url);
    //var infoLink = "http://api.tumblr.com/v2/blog/yaimg.tumblr.com/info?";
    var infoLink = "http://api.tumblr.com/v2/blog/" + url + ".tumblr.com/info?";
    //var photoLink = "http://api.tumblr.com/v2/blog/yaimg.tumblr.com/posts?";
    var photoLink = "http://api.tumblr.com/v2/blog/" + url + ".tumblr.com/posts?";
    
    $('#viewer').empty();
    $('#viewer').append("<p align=center style='font-size:12px'> <b>...LOADING...</b></p>");
    
    $.ajax({
        type: "GET",
        url : infoLink,
        dataType: "jsonp",
        data: {
            // http://www.tumblr.com/oauth/apps :: FriiNoView OAuth consumer key:
            api_key: "Vw6ftsZVq4oUg9vK7ZGOKGThqGQ2xfsRr7cy0MyKYvEC4dot14",
        }
    }).done(function( data ) {
        _totalNo = data.response.blog.posts;
        
        // 0 ~ (totalNo-1) random
        var rnd = 0 + Math.floor(Math.random() * _totalNo) ;
        
        $.ajax({
            type: "GET",
            url : photoLink,
            dataType: "jsonp",
            data: {
                // http://www.tumblr.com/oauth/apps :: FriiNoView OAuth consumer key:
                api_key: "Vw6ftsZVq4oUg9vK7ZGOKGThqGQ2xfsRr7cy0MyKYvEC4dot14",
                limit:1,
                offset:rnd
            }
        }).done(function( data ) {
            $('#viewer').empty();
            //$('#viewer').append("<a href='" + this.post_url + "'> src </a>");
            
            $.each(data.response.posts, function(){
                var curURL = this.post_url;
                var cur = "<a target=_blank href='" + curURL + "'><b>" + rnd + "</b> in <b>" + _totalNo + "</b></a>";
                var sep = " &nbsp; / &nbsp; ";
                var parg = "<p align=right style='margin:0 2em 1.5em'>";
                $('#viewer').append( parg + prev2tmbrUrl + sep + prev1tmbrUrl + sep + cur + "</p>");
                prev2tmbrUrl = prev1tmbrUrl ;
                prev1tmbrUrl = cur;
                
                var _photos = this.photos;
                $.each(_photos, function(){
                    // 1280 or max size
                    //$('body').append("<img src='" + this.original_size.url + "'>");
                    //400 or less reduced size
                    $('#viewer').append("<p align=center style='margin-top:-3px'><a target=_blank href='" + curURL + "'><img src='" + this.alt_sizes[1].url + "'></a></p>");
                });
                
                var _tags = this.tags;
                $('#viewer').append("<p align=center style='margin-top:-3px ; font-size:12px'>" + _tags.join(" / ") + "</p>");
            });
        });
    });
 }